package main

import (
	"fmt"
	"strconv"
)

// RemoveDuplicate 数组去重复-一维数组去重
func RemoveDuplicate[T comparable](sliceList []T) []T {
	allKeys := make(map[T]bool)
	result := make([]T, 0, len(sliceList))

	for _, item := range sliceList {
		if _, value := allKeys[item]; !value {
			allKeys[item] = true

			result = append(result, item)
		}
	}

	return result
}

// RemoveDuplicateCustom 数组去重复（自定义比较）
func RemoveDuplicateCustom[T, K comparable](sliceList []T, keyFunc func(T) K) []T {
	allKeys := make(map[K]bool)
	result := make([]T, 0, len(sliceList))

	for _, item := range sliceList {
		key := keyFunc(item)
		if _, value := allKeys[key]; !value {
			allKeys[key] = true
			result = append(result, item)
		}
	}
	return result
}

// 泛型学习
func arrDeal() {
	fmt.Println("数组去重学习")
	a := []string{"hello", "a", "b", "hello", "a", "dfad", "da", "daff"}
	as := RemoveDuplicate(a)
	fmt.Println(as)
}

// 定义使用二维切片 数组里面带数组
func useTwos() {
	acc := make([][]string, 0)
	for i := 0; i < 10; i++ {
		a := make([]string, 0)
		for j := 0; j < 10; j++ {
			a = append(a, "hello"+strconv.Itoa(j))
		}
		acc = append(acc, a)
	}
	fmt.Println(acc)
}

// 定义二维切片 数组里面带map
func useTwoMap() {
	//定义数据 [{"name":"jack"},{"name":"ankile"}]
	var ab []map[string]interface{}
	a := make(map[string]interface{})
	a["name"] = "jack"
	ab = append(ab, a)
	a["name"] = "ankile"
	ab = append(ab, a)
	ab = append(ab, a)
	fmt.Println(ab)
	a["name"] = "anxiong"
	ab = append(ab, a)
	// 数组去重

}

func main() {
	fmt.Println("泛型学习 ")
	//arrDeal()
	useTwoMap()
}
